<?php
namespace app\admin\controller;
use app\admin\controller\AdminController;
/**
 * 注册会员管理
 */
class UsersController extends AdminController
{

    /**
     * 当前模块参数
     */
    protected function _infoModule()
    {
        return array(
            'info'  => array(
                'name' => '注册会员列表',
                'description' => '查看网站所有注册会员信息',
            ),
            'menu' => array(
                array(
                    'name' => '会员列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            'add' => array(
                array(
                    'name' => '新增会员',
                    'url' => url('add'),
                ),
            ),
        );
    }

    /**
     * 列表
     */
    public function index(){
        //查询数据
        $list = target('Users')->loadList();
        //位置导航
        $breadCrumb = array('注册会员列表'=>url());
        //模板传值
        $this->assign('breadCrumb',$breadCrumb);
        $this->assign('list',$list);
        $this->adminDisplay();
    }

    /**
     * 增加
     */
    public function add(){
        if(!IS_POST){
            $breadCrumb = array('会员列表'=>url('index'),'添加'=>url());
            $this->assign('breadCrumb',$breadCrumb);
            $this->assign('name','添加');
            $this->adminDisplay('info');
        }else{
            if(target('users')->saveData('add')){
                $this->success('会员添加成功！',url('index'));
            }else{
                $msg = target('users')->getError();
                if(empty($msg)){
                    $this->error('会员添加失败');
                }else{
                    $this->error($msg);
                }

            }
        }
    }

    /**
     * 修改
     */
    public function edit(){
        if(!IS_POST){
            $userId = request('get.uid','','intval');
            if(empty($userId)){
                $this->error('参数不能为空！');
            }
            //获取记录
            $model = target('Users');
            $info = $model->getInfo($userId);
            if(!$info){
                $this->error($model->getError());
            }
            $breadCrumb = array('会员列表'=>url('index'),'修改'=>url('',array('uid'=>$userId)));
            $this->assign('breadCrumb',$breadCrumb);
            $this->assign('name','修改');
            $this->assign('info',$info);
            $this->adminDisplay('info');
        }else{
            if(target('Users')->saveData('edit')){
                $this->success('会员修改成功！',url('index'));
            }else{
                $msg = target('Users')->getError();
                if(empty($msg)){
                    $this->error('会员修改失败');
                }else{
                    $this->error($msg);
                }
            }
        }
    }

    /**
     * 删除
     */
    public function del(){
        $userId = request('post.data');
        if(empty($userId)){
            $this->error('参数不能为空！');
        }
        //获取会员数量
        if(target('Users')->delData($userId)){
            $this->success('会员删除成功！');
        }else{
            $msg = target('Users')->getError();
            if(empty($msg)){
                $this->error('会员删除失败！');
            }else{
                $this->error($msg);
            }
        }
    }

    public function audit(){
        $uid = request("get.uid");
        $status = request("get.status");
        $save_data = array(
            'uid' => $uid,
            'status' => $status
        );
        if(target('Users')->upUserData($save_data)){
            $this->success("会员状态变更成功！");
        }else{
            $msg = target("Users")->getError();
            if(empty($msg)){
                $this->error('会员信息变更失败！');
            }else{
                $this->error($msg);
            }
        }
    }
}